Skip to main content

Typedef command

Introduction

The typedef command is used to give an alias to a type.

typedef type name;

In the above code, type stands for the type name and name for the alias.

typedef unsigned char BYTE;

BYTE c = 'z';

In the example above, the typedef command gives the type unsigned char the alias BYTE, and you can then declare the variable using BYTE.

Typedef can specify multiple aliases at once.

typedef int antelope, bagel, mushroom;

In the above example, three aliases are given to the int type at once.

typedef can be used to alias pointers.

typedef int* intptr;

int a = 10;
intptr x = &a;

In the example above, intptr is an alias for int*. However, be careful when using it so that it is not easy to see that the variable x is a pointer type.

typedef can also be used to alias an array type.

typedef int five_ints[5];

five_ints x = {11, 22, 33, 44, 55};

In the above example, five_ints is an array type containing 5 integers of

typedef to alias a function is written as follows.

typedef signed char (*fp)(void);

In the above example, the type alias fp is a pointer to the function signed char (*)(void).

Main benefits

The main benefits of typedef for aliasing types are the following.

(1) Better code readability.

typedef char* STRING;

STRING name;

The above example aliases a character pointer to STRING, so that when you use STRING to declare a variable in the future, you can easily identify the variable as a string.

(2) Create aliases for complex data structures defined by commands such as struct, union, enum, etc., so that they can be easily referenced.

struct treenode {
// ...
};

typedef struct treenode* Tree;

In the above example, Tree is an alias for struct treenode*.

typedef can also be written together with the command struct defines the data type.

typedef struct animal {
char* name;
int leg_count, speed;
} animal;

In the above example, the custom data type is given an alias animal for struct animal using the typedef command at the same time.

In this case, C allows the type name after the struct command to be omitted.

typedef struct {
char *name;
int leg_count, speed;
} animal;

The above example is equivalent to aliasing an anonymous data type to animal.

(3) typedef is convenient for changing the type of a variable later.

typedef float app_float;

app_float f1, f2, f3;

In the above example, the variables f1, f2 and f3 are all of type float. If you need to change the type for them later, you can simply change the typedef statement.

typedef long double app_float;

The above command changes the type of the variables f1, f2 and f3 to long double.

(4) Portability

The type of a particular value may be different on different computers.

int i = 100000;

The above code is fine on a computer with 32-bit integers, but it will be wrong on a computer with 16-bit integers.

The solution in C is to provide type aliases that will be interpreted as different types on different computers, e.g. int32_t.

int32_t i = 100000;

The above example declares the variable i as type int32_t, ensuring that it will be 32 bits wide on different computers and that there will be no errors when porting the code.

Type aliases of this type are defined using typedef. Here is a similar example.

typedef long int ptrdiff_t;
typedef unsigned long int size_t;
typedef int wchar_t;

These integer type aliases are placed in the header file stdint.h, and computers of different architectures simply modify this header file without modifying the code.

Thus, typedef helps to improve the portability of the code and make it adaptable to computers of different architectures.

(5) Simplifying type declarations

Some type declarations in C are quite complex, such as the one below.

char (*(*x(void))[5])(void);

typedef simplifies complex type declarations and makes them easier to understand. First, the outermost layer starts with a type alias.

typedef char (*Func)(void);
Func (*x(void))[5];

This still looks a bit complicated, just define an alias for the inner layer as well.

typedef char (*Func)(void);
typedef Func Arr[5];
Arr* x(void);

The above code is a little easier to decipher.

  • x is a function that returns a pointer to an Arr type.
  • Arr is an array with 5 members, each of type Func.
  • Func is a function pointer to a function that has no arguments and returns a character value.